import json

import scrapy
from urllib.parse import urlencode

class RequestMethodSpider(scrapy.Spider):
    name = "request_method"

    # start_urls = ["https://bai.com"]

    # get请求带参数  没有pararm参数  要？ 和urlencode
    # async def start(self):
    #     yield scrapy.Request(f'http://httpbin.org/get?a=10&{urlencode({"b": 10,"e":20})}',callback=self.parse_p)




    # # Content-Type": "application/json 默认是这个 要用json.dumps
    # async def start(self):
    #     yield scrapy.Request(f'http://httpbin.org/post?a=30', callback=self.parse,method="POST",
    #                          headers={"Content-Type": "application/json"},
    #                          body=json.dumps({"hhh":30}))
    #
    # # 改成"Content-Type": "application/x-www-form-urlencoded"  用urlencode
    # async def start(self):
    #     yield scrapy.Request(f'http://httpbin.org/post?a=30',callback=self.parse, method="POST",
    #                          headers = {"Content-Type": "application/x-www-form-urlencoded"},
    #                          body=urlencode({"a": "30"}))
    #
    # 这个就直接是post  formdata代替了body直接传数据 Content-Type": "application/x-www-form-urlencoded"
    # async def start(self):
    #     yield scrapy.FormRequest(f'http://httpbin.org/post?a=30',formdata={'qqq':"111111"})
    #

    # def parse_p(self, response):
    #     print(response.json(),'11111111111')

    # def parse(self, response):
    #     print(response.json())




    # 参数的传递 cb_kwargs 和 meta

    async def start(self):
        yield scrapy.FormRequest('http://httpbin.org/get',callback=self.parse ,cb_kwargs={'age':10},meta={'name':'zzf'})



    def parse(self, response,age):
        print(age,response.meta['name'])
        response.meta.update({'sex':'nan'})

        yield scrapy.FormRequest('http://httpbin.org/get?a=1', callback=self.parse2,cb_kwargs={'age':age},meta = response.meta)
    def parse2(self,response,age):
        print(age,'111')
        print(response.meta)
